/*
GANESHA - Reasoning on critical infrastructures affected by emergency situations
Copyright (c) 2010-2011, Information Eng. Research Unit - Univ. of Alcala
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package es.uah.cc.ie.ganesha.messaging.tso;

import es.uah.cc.ie.ganesha.messaging.AlertMessage;
import es.uah.cc.ie.ganesha.messaging.position.PointPosition;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tacticalsituationobject.schemas.tso.x20.COORDType;
import org.tacticalsituationobject.schemas.tso.x20.TSO20Document;
import org.tacticalsituationobject.schemas.tso.x20.TSO20Type;

/**
 * Encapsulates an alert message based on TSO protocol.
 * @author msicilia
 */
public class TSOMessage extends AlertMessage {

	protected TSO20Type _originalTSO;

        /**
         * Constructor
         * @param identifier Identifier
         */
        public TSOMessage(String identifier) {
		super(identifier);
	}

        /**
         * Constructor
         * @param tso TSO object parsed with XML Beans
         */
        public TSOMessage(TSO20Type tso) {
		_identifier = tso.getCONTEXT().getID();
		assert tso.getEVENT().getETYPE().getCATEGORYArray().length > 0;
		_eventType = tso.getEVENT().getETYPE().getCATEGORYArray()[0];

		assert tso.getEVENT().getEGEOArray().length > 0 && tso.getEVENT().getEGEOArray()[0].getPOSITION().getCOORDArray().length > 0;
		COORDType coord = tso.getEVENT().getEGEOArray()[0].getPOSITION().getCOORDArray()[0];

		_positions.add(new PointPosition(coord.getLAT(), coord.getLON()));

		_originalTSO = tso;
	}

	/**
	 * Returns the correspondant XMLBeans object.
	 * @return Returns the correspondant XMLBeans object.
	 */
	public TSO20Type toXMLObject() {
		TSO20Type result = TSO20Type.Factory.newInstance();

		result.addNewCONTEXT().setID(_identifier);

		result.addNewEVENT().addNewETYPE().addCATEGORY(_eventType);
		COORDType newCoords = result.getEVENT().addNewEGEO().addNewPOSITION().addNewCOORD();
		if(_positions.size() > 0)
		{
			newCoords.setLAT(_positions.get(0).getCenterLatitude());
			newCoords.setLON(_positions.get(0).getCenterLongitude());
		}

		return result;
	}

	/**
	 * Writes the message to an XML file.
	 * @param path XML file path.
	 */
	public void writeToXMLFile(String path) {
		try {
			TSO20Document document = TSO20Document.Factory.newInstance();
			document.addNewTSO20().set(this.toXMLObject());
			Writer output = null;
			File file = new File(path);
			output = new BufferedWriter(new FileWriter(file));
			output.write(document.toString());
			output.close();
		} catch (Exception ex) {
			Logger.getLogger(TSOMessage.class.getName()).log(Level.SEVERE, null, ex);
		}
	}
}
